package com.mycompany.videoclub;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Peliculadaoimpl implements Peliculadao {
    
    public Conectar conexion;
    
    public Peliculadaoimpl(){
        conexion = new Conectar();
    }
    
    public List<Pelicula> list(String valor) throws IOException {
        final String queryLista = "SELECT * FROM peliculas where CONCAT (Genero,'',Nombre,'',Director) LIKE '%"+valor+"%'";
        List listaPeliculas   = new ArrayList<Pelicula>();
        
        try{
            Connection cn = conexion.conexion();
            PreparedStatement st = cn.prepareStatement(queryLista);
            
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Pelicula p = new Pelicula();
                p.setID(rs.getInt(1));
                p.setNombre(rs.getString(2));
                p.setDescripcion(rs.getString(3));
                p.setGenero(rs.getString(4));
                p.setDirector(rs.getString(5));
                p.setActores_Principales(rs.getString(6));
                p.setDuracion(rs.getInt(7));
                listaPeliculas.add(p);
            }
            st.close();
            cn.close();
        } catch (SQLException e){
            System.out.println("Error:"+ e.getMessage());
            e.printStackTrace();
        }
        return listaPeliculas;
    }
    
    public String insert(Pelicula pelicula) throws IOException {
        String result = null;
        String sql = "INSERT INTO peliculas (Nombre, Descripcion, Genero, Director, Actores_Principales, Duracion)"+"Values(?,?,?,?,?,?)";
        
        try {
            Connection cn = conexion.conexion();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, pelicula.getNombre());
            ps.setString(2, pelicula.getDescripcion());
            ps.setString(3, pelicula.getGenero());
            ps.setString(4, pelicula.getDirector());
            ps.setString(5, pelicula.getActores_Principales());
            ps.setInt(6, pelicula.getDuracion());
            ps.executeUpdate();
            ps.close();
            cn.close();
        } catch (SQLException e) {
            result = e.getMessage();
        }
        return result;
    }
    
    public Pelicula getid(Integer ID) throws IOException {
        Pelicula pelicula = null;
        String sql = "select * from peliculas where ID=?";
        
        try {
            Connection cn = conexion.conexion();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, ID);
            ResultSet rs = ps.executeQuery();
            pelicula = new Pelicula();
            if (rs.next()) {
                pelicula.setID(rs.getInt(1));
                pelicula.setNombre(rs.getString(2));
                pelicula.setDescripcion(rs.getString(3));
                pelicula.setGenero(rs.getString(4));
                pelicula.setDirector(rs.getString(5));
                pelicula.setActores_Principales(rs.getString(6));
                pelicula.setDuracion(rs.getInt(7));
            }
            cn.close();
            ps.close();
        } catch (SQLException e) {
            System.out.println("Error: " + e.getMessage());
        } 
        return pelicula;
}

    public String delete(Integer ID) throws IOException {
        String result=null;
        String sql="delete from peliculas where ID=?";
        
        try {
            Connection cn=conexion.conexion();
            PreparedStatement ps=cn.prepareStatement(sql);
            ps.setInt(1, ID);
            ps.executeUpdate();
            cn.close();
            ps.close();
        } catch (SQLException e) {
            result=e.getMessage();
        }
        return result;
    }
    
    public Pelicula get(Integer id) {
        Pelicula pelicula = null;
        
        String sql = "SELECT * from peliculas where ID = ?";
        
        try{
            Connection cn = null;
            try {
                cn = conexion.conexion();
            } catch (IOException ex) {
                Logger.getLogger(Peliculadaoimpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet  rs = ps.executeQuery();
            pelicula = new Pelicula();
            if(rs.next()){
                  pelicula.setID(rs.getInt(1));
                  pelicula.setNombre(rs.getString(2));
                  pelicula.setDescripcion(rs.getString(3));
                  pelicula.setGenero(rs.getString(4));
                  pelicula.setDirector(rs.getString(5));
                  pelicula.setActores_Principales(rs.getString(6));
                  pelicula.setDuracion(rs.getInt(7));
            }
            cn.close();
            ps.close();
       }
       catch(SQLException ex){
                System.out.println("Error:"+ ex.getMessage());
       }

        return pelicula;
    }
    
    public String update(Pelicula pelicula) throws IOException {
        String result=null;
        String sql= "UPDATE peliculas SET Nombre=?, Descripcion=?, Genero=?, Director=?, Actores_Principales=?, Duracion=? WHERE ID=?";
              
        try {
            Connection cn=conexion.conexion();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, pelicula.getNombre());
            ps.setString(2, pelicula.getDescripcion());
            ps.setString(3, pelicula.getGenero());
            ps.setString(4, pelicula.getDirector());
            ps.setString(5, pelicula.getActores_Principales());
            ps.setInt(6, pelicula.getDuracion());
            ps.setInt(7, pelicula.getID());
            ps.executeUpdate();
            ps.close();
            cn.close();
        } catch (SQLException e) {
            result=e.getMessage();         
        }
        return result;
    }
}